﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using NHibernate;

using ProjectBase.Core;

namespace $safeprojectname$.NH
{
    /// <summary>
    ///     NHibernate session accessor class
    /// </summary>
    public static class NHSession
    {
        public static ISession GetCurrent(string factoryName)
        {
            if (!Global.Settings.EnableNHDao)
            {
                return null;
            }
            return NHSessionFactory.GetCurrentSession(factoryName);
        }

        /// <summary>
        ///     Provides the access to currently existing transaction property 'IsActive'
        /// </summary>
        public static bool TransactionIsActive
        {
            // Wraper: No need to keep NHibernate .dll reference in calling project needed for ISession!
            get
            {
                var isActive = false;

                foreach (var factoryName in NHSessionFactory.FactoryNames)
                {
                    var session = GetCurrent(factoryName);
                    if (session.Is())
                    {
                        isActive |= session.Transaction.IsActive;
                    }
                }
                return isActive;
            }
        }

        /// <summary>
        ///     Provides the access to currently existing ISession's "BeginTransaction()";
        /// </summary>
        public static void TransactionBegin()
        {
            // Wraper: No need to keep NHibernate .dll reference in calling project needed for ISession!

            foreach (var factoryName in NHSessionFactory.FactoryNames)
            {
                var session = GetCurrent(factoryName);
                if (session.Is())
                {
                    session.BeginTransaction();
                }
            }
        }

        /// <summary>
        ///     Provides the access to currently existing ISession's "Transaction.Commit()";
        /// </summary>
        public static void TransactionCommit()
        {
            // Wraper: No need to keep NHibernate .dll reference in calling project needed for ISession!

            try
            {
                foreach (var factoryName in NHSessionFactory.FactoryNames)
                {
                    var session = GetCurrent(factoryName);
                    if (session.Is())
                    {
                        session.Flush();
                    }
                }

                foreach (var factoryName in NHSessionFactory.FactoryNames)
                {
                    var session = GetCurrent(factoryName);
                    if (session.Is())
                    {
                        session.Transaction.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                PublishingManager.Publish(null, "Dao Commit transaction Exception", ex);
                foreach (var factoryName in NHSessionFactory.FactoryNames)
                {
                    var session = GetCurrent(factoryName);
                    if (session.Is())
                    {
                        session.Transaction.Rollback();
                    }
                }
            }
        }

        /// <summary>
        ///     Provides the access to currently existing ISession's "Transaction.Rollback()";
        /// </summary>
        public static void TransactionRollback()
        {
            // Wraper: No need to keep NHibernate .dll reference in calling project needed for ISession!

            foreach (var factoryName in NHSessionFactory.FactoryNames)
            {
                var session = GetCurrent(factoryName);
                if (session.Is())
                {
                    session.Transaction.Rollback();
                }
            }
        }

        public static void Clear()
        {
            if (!Global.Settings.EnableNHDao)
            {
                return;
            }
            foreach (var factoryName in NHSessionFactory.FactoryNames)
            {
                var session = GetCurrent(factoryName);
                if (session.Is())
                {
                    session.Clear();
                }
            }
        }

        public static void Dispose()
        {
            if (!Global.Settings.EnableNHDao)
            {
                return;
            }
            foreach (var factoryName in NHSessionFactory.FactoryNames)
            {
                NHSessionFactory.DisposeCurrentSession(factoryName);
            }
        }
    }
}